package com.ehotting.edsta.mqbd.monitor.task.active.module;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ehotting.eaf.core.utils.OkHttpUtils;
import com.ehotting.edsta.mqbd.api.enums.SysModuleEnum;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.core.logger.LOGGER_KEY;
import com.ehotting.mqbd.core.logger.ModuleActiveLogger;
import com.ehotting.mqbd.core.monitor.ModuleActiveAble;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.List;

public class DiagnosisPredictActive implements ModuleActiveAble {

    private static Logger logger = LoggerFactory.getLogger(DiagnosisPredictActive.class);

    private static String URL = "/aiplatform/traininginfo/training-info/getTrainingStatusList";

    private ModuleActiveLogger mqActiveLogger;

    public DiagnosisPredictActive(ModuleActiveLogger mqActiveLogger){
        this.mqActiveLogger = mqActiveLogger;
    }

    public String getModule(){
        return SysModuleEnum.DIAGNOSIS_PREDICT.getCode();
    }

    public  Integer doAction(SolverStateBean solverState){
        String requestUrl = "http://"+solverState.getSolverInfo().getIp()+":"+solverState.getSolverInfo().getPort()+ URL
                +"?ip="+solverState.getSolverInfo().getIp();
        MDC.put(LOGGER_KEY.URL,requestUrl);
        logger.info("requestUrl:{}",requestUrl);
        String result = OkHttpUtils.builder(5,5,5).url(requestUrl).get().sync();
        MDC.put(LOGGER_KEY.RESPONSE,result);
        logger.info("result:{}",result);
        return getUnFinish(result);
    }

    @Override
    public ModuleActiveLogger getModuleActiveLogger() {
        return mqActiveLogger;
    }


    public  Integer getUnFinish(String s){

        ResultData resultData = (ResultData) JSON.parseObject(s, ResultData.class);
        if(resultData.getStatus().equals(200)){
            return getUnFinish(resultData.getData());
        }
        return -1;
    }

    private static Integer getUnFinish(List<JSONObject> data){
        Integer unStart = 0;
        Integer unFinish = 0 ;
        if(data!=null && data.size()>0){
            for(JSONObject json : data){
                if(json.containsKey("task1")){
                    unStart = json.getInteger("num");
                }
                if(json.containsKey("task2")){
                    unFinish = json.getInteger("num");
                }
            }
        }
        return unStart + unFinish;
    }

    public static class ResultData {
        private Integer status;
        private String msg;
        private Boolean success;
        private List<JSONObject> data;

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public Boolean getSuccess() {
            return success;
        }

        public void setSuccess(Boolean success) {
            this.success = success;
        }

        public List<JSONObject> getData() {
            return data;
        }

        public void setData(List<JSONObject> data) {
            this.data = data;
        }
    }

}